const app = getApp()
import { currentDates, time, timeBefter } from '../../utils/util'
Page({
  data: {
    imgurl: app.globalData.imgurl,
    city: [
      { name: '生日/团体', id: 0 },
      { name: '玩习课', id: 1 },
    ],
    active: 0,
    url: 'party',
    page: 1,
    list: [],
  },
  onLoad() {
    this._getData()
  },
  // _tapTabsClick({ detail: { index } }) {
  //   this.setData({
  //     active: index,
  //     page: 1,
  //   })
  // },
  _tapTabs({ detail: { index } }) {
    let { active } = this.data
    this.setData({
      active: index,
      page: 1,
      list: [],
    })
    if (active != index && index == 1) {
      this.setData({
        url: 'game',
      })
      this._getData()
    } else if (active != index && index == 0) {
      this.setData({
        url: 'party',
      })
      this._getData()
    }
  },
  async _getData() {
    const { page, list, url } = this.data
    let { code, data } = await wx.requestAsync({
      url: `/pre/${url}/list`,
      data: {
        page,
        limit: 10,
      },
    })
    if (code == 0) {
      let arr = data.map((item) => {
        item.avatar = this.data.imgurl + item.avatar
        item.pre_at =
          currentDates(item.pre_at * 1000) +
          ' ' +
          time(item.pre_at) +
          (item.hour ? '-' + timeBefter(item.pre_at, item.hour) : '')
        return item
      })
      this.setData({
        list: [...list, ...arr],
      })
    }
  },
  // 去使用
  _toStore({
    currentTarget: {
      dataset: { sid },
    },
  }) {
    wx.navigateTo({
      url: '/pages/storeIntroduction/storeIntroduction?s_id=' + sid,
    })
  },
  // 上拉加载
  onReachBottom() {
    let { page } = this.data
    this.setData({ page: page + 1 })
    this._getData()
  },
})
